﻿using RTW.Core.Ioc;
using RTW.Core.Repository;
using RTW.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RTW.Servers
{
    public class DeptServers : BaseServers<SysDept>, IAutoInject
    {
        private readonly IRepositoryAsync<TDepartment> Sys_MenuRepository;

        private readonly IRepositoryAsync<SysDept> SysDeptRepository;
        public DeptServers()
        {
            Sys_MenuRepository = new DapperAsyncRepository<TDepartment>();
            SysDeptRepository=new DapperAsyncRepository<SysDept>();
        }


        public async Task<List<TDepartment>> GetTDepartment()
        {
            return (await Sys_MenuRepository.GetAllEnumerableAsync()).ToList();
        }

        /// <summary>
        /// 根据单位ID获取数据
        /// </summary>
        /// <param name="DWID"></param>
        /// <returns></returns>
        public async Task<List<SysDept>> GetDeptWhere(int? DWID)
        {
            return (await SysDeptRepository.SqlWithParams("select * from Sys_Dept  where DWID=:DWID", new { DWID = DWID })).ToList();
        }
        /// <summary>
        /// 得到下拉框的数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<SysDept>> GetDeptSelect()
        {
            return (await SysDeptRepository.SqlWithParams("select * from Sys_Dept  where BMZT !=0")).ToList();
        }
        public async Task<List<SysDept>> GetDeptIDWhere(int? BMID)
        {
            return (await SysDeptRepository.SqlWithParams("select * from Sys_Dept  where BMID=:BMID", new { BMID = BMID })).ToList();
        }
        public async Task<List<SysDept>> GetAllDepts()
        {
            return (await SysDeptRepository.GetAllEnumerableAsync()).ToList();
        }

        public async Task<SysDept> GetDept(int? id)
        {
            return await SysDeptRepository.GetByIdAsync(id);
        }

        public async Task<bool> AnyByName(object name)
        {
            return await SysDeptRepository.SqlWithParamsBool("select * from Sys_Dept  where BMMC=:BMMC", new { BMMC = name });
        }

        public async Task<List<SysDept>> GetDeptID()
        {
            return (await SysDeptRepository.SqlWithParams("select top(1) BMID from Sys_dept order by BMID desc")).ToList();
        }

        public async Task<bool> AnyByCode(string code)
        {
            return await SysDeptRepository.SqlWithParamsBool("select * from Sys_Dept  where DWDM=:DWDM", new { DWDM = code });
        }
    }
}
